$objects = []
def add_object obj,a
      a.indices=[]
      $objects << obj
      node000 = Node::new(5,NODE_TYPE_EXPANDITEM,a) 
      node = node000
      link_to = obj.home
      dbref,name,location,type,flags = obj.dbref,obj.name,obj.location,obj.type,obj.flags
      rarr = [dbref,
      name,
      location,
      link_to,
      type,
      flags.join(" ")]
      node.iter = rarr
      nit = []
      fill_node(obj,node,a)
      fill_locks(obj,a,nit)
      obj.attributes.each_key {|key|
        fill_atts(obj,key,a,nit)
      }
      lst = [":AT",":AND"]
      obj.atty_node.get_children.each_with_index {|jnode,i|
        nit << [jnode,[obj.dbref,lst[i],"","","",""]]
      }
      a.add_node(node000)
      node000.unexpand node000
      nit.each {|set|
        set[0].iter = set[1]
      }
      node000.unexpand node000
      updateList a
  end

def fill_node(obj,node,a)
      atty_node = Node::new(5,NODE_TYPE_EXPANDITEM,a,node) 
      root_node = node
      atty_node.iter[0]=obj.dbref
      atty_node.iter[1]="ATTRIBUTES"
      lock_node = Node::new(5,NODE_TYPE_EXPANDITEM,a,root_node) 
      iter = lock_node.iter
      iter[0]=obj.dbref
      iter[1]="LOCKS"
      at_node = Node::new(5,NODE_TYPE_EXPANDITEM,a,atty_node) 
      and_node = Node::new(5,NODE_TYPE_EXPANDITEM,a,atty_node) 
      lst = [":AT",":AND"]
      setObjectNode obj,root_node,at_node,and_node,lock_node,atty_node
end

def setObjectNode obj,root_node,at_node,and_node,lock_node,atty_node
  obj.instance_variable_set("@root_node",root_node)
  obj.instance_variable_set("@at_node",at_node)
  obj.instance_variable_set("@and_node",and_node)
  obj.instance_variable_set("@lock_node",lock_node)
  obj.instance_variable_set("@atty_node",atty_node)
  def obj.root_node
    return @root_node
  end
  def obj.at_node
    return @at_node
  end
  def obj.and_node
    return @and_node
  end
  def obj.lock_node
    return @lock_node
  end
  def obj.atty_node
    return @atty_node
  end
end

def fill_atts obj,key,a,nit
      case key
        when :at
          node = obj.at_node
        when :and
          node = obj.and_node
      else
      end
      obj.attributes[key].each_key {|attr|
        n = Node::new(5,NODE_TYPE_ITEM,a,node) 
        atty = obj.attributes[key][attr][:data]
        case atty.type.to_s
          when "at"
            type = "@"
          when "and"
            type = "&"
        else
          type = atty.type.to_s
        end
        nit << [n,[obj.dbref,atty.name.to_s,"","",type,atty.flags.join(" ")]]
      }
end

def fill_locks obj,a,nit
  node = obj.lock_node
  obj.locks::each_key {|l|
    n = Node::new(5,NODE_TYPE_ITEM,a,node) 
    nit << [n,[obj.dbref,"","","",l.to_s,obj.locks[l][:flags].join(" ")]]
  }
end
#####################################################################################################33
#
#########################################################################################################
def editor_load_file
  editor = $app.instance_variable_get("@styled")
  d = Wx::FileDialog::new editor,"Choose file..."
  d.show_modal
  d.set_path(Dir.getwd)
  f=d.get_path
end
def create_intermediateObject_yamlFile robj
  obj = robj.clone
# p obj
# p robj
  obj.connection = nil
  obj.instance_variable_set("@atty_node",nil)
  obj.instance_variable_set("@and_node",nil)
  obj.instance_variable_set("@at_node",nil)
  obj.instance_variable_set("@lock_node",nil)
  obj.instance_variable_set("@root_node",nil)
  name = obj.dbref+".iof"
  begin
    Dir.mkdir "data/"+$client.client.host+"--"+$client.client.port
  rescue

  end
  File::open("data/"+$client.client.host+"--"+$client.client.port+"/"+name,'w') {|f|
    f.puts obj.to_yaml
  }
end
def get_project(name)
	pj = $projects.find {|p1| p1[0]==name }
        pj[1]
end
def get_pobject(q)
	pj = $pobjects.find {|p1| p1[0]==q }
        pj[1]	
end
#################################################################################################3
#
##################################################################################################
def setup_inputs(ui,styled)
	ui.input.evt_key_up {|e| handle_key(e,ui.input)}
	styled.evt_key_up {|e| handle_key(e,styled)}
#         evt_button(@m_button12.get_id) {|e| getLogInString() }
	# @m_textctrl11.evt_key_up {|e| handle_key(e,@m_textctrl11)}
	ui.output.background_colour = Wx::Colour::new("BLACK")
#         extend()
        $history = {ui.input=>History::new,
                    styled=>History::new
#                     @m_textctrl12=>History::new
                   }
end
def handle_key(e,widget)
    if ( e.get_modifiers == Wx::MOD_CONTROL )
      if widget.class == MuIDE::Input
        str = widget.value
        code = e.get_key_code
      else
        str = widget.get_text
        code = e.get_key_code
      end
     case code
       when 80
         if widget.class == MuIDE::Input
           widget.value = $history[widget].get_prev
         else
           widget.set_text($history[widget].get_prev)
         end
       when 78
         if widget.class == MuIDE::Input
           widget.value = $history[widget].get_next
         else
           widget.set_text($history[widget].get_next)
         end
       when 83
         vars = []
         $history[widget].enter(str)
         sstr=str.clone
         while sstr =~ /\<code\>(.*?)\<\/code\>/
p $1
           d = $1
           e = Eval::new($1)
           e.evaluate
           sstr.gsub!("<code>#{d}</code>",e.value)
p str
         end
         $client.client.socket.puts sstr
         if widget.class == MuIDE::Input
           widget.value = ""
         else
           widget.set_text("")
         end
       when 69
         $history[widget].enter(str)
         safe_eval str
     else e.skip
     end
  end
end

def safe_eval str
         begin
           eval str
         rescue Exception => er
	  err = "Error: #{er.to_s}\n"
	  err += "Platform: #{Wx::PLATFORM}\n"
	  err += "MuIDE Version: #{VERSION}\n"
	  err += "Ruby Version: #{RUBY_VERSION}\n"
	  err += "Backtrace:\n\t"
	  err += er.backtrace.join("\n\t")
	  File.open("evalMuIDE.err","w") do |fh|
		fh.print(err)
	  end
	  #dir = File.dirname(File.expand_path(__FILE__))
	  if Wx::PLATFORM == "WXMSW"
		Kernel.exec("rubyw #{Dir.getwd}\\libs\\gui\\main\\wxEvalError.rb")
	  else
                fork do
		  system("ruby libs/gui/main/wxEvalError.rb")
                end
 	  end
        end
end